package com.gj.baselib.powermanager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Menu;
import android.view.View;

import com.gj.baselib.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Flag Value 		     	 CPU 		Screen 		Keyboard 
 *PARTIAL_WAKE_LOCK 		 On* 		  Off 		 Off 
 *SCREEN_DIM_WAKE_LOCK 	 	 On 		  Dim 		 Off 
 *SCREEN_BRIGHT_WAKE_LOCK 	 On 		 Bright 	 Off 
 *FULL_WAKE_LOCK 			 On 		 Bright 	Bright 
 */
public class PowerManagerActivity extends Activity {

	private WakeLock wakeLock = null;
	private PowerManager pm = null;
	
	@OnClick(R.id.btn_PARTIAL)
	private void btnPARTIALClick(View v) {
		releaseWakeLock();
		
		wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getCanonicalName());
		wakeLock.acquire();
	}

	private void releaseWakeLock() {
		if (wakeLock != null && wakeLock.isHeld()) {
			wakeLock.release();
			wakeLock = null;
		}
	}

	@OnClick(R.id.btn_DIM)
	private void btnDIMClick(View v) {
		releaseWakeLock();
		
		wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass().getCanonicalName());
		wakeLock.acquire();
	}

	@OnClick(R.id.btn_BRIGHT)
	private void btnBRIGHTClick(View v) {
		releaseWakeLock();
		
		wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, this.getClass().getCanonicalName());
		wakeLock.acquire();
	}

	@OnClick(R.id.btn_FULL)
	private void btnFULLClick(View v) {
		releaseWakeLock();
		
		wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, this.getClass().getCanonicalName());
		wakeLock.acquire();
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_powermanager);

		ViewUtils.inject(this);

		pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
